<?php

namespace Biz\tools;

use Biz\cache\ChinaArea as cChinaArea;
use hhz\constant\redis\redisKeys;
use Hhz\Redis\newCache;

class ShopArea
{
    private static $areaData = [];

    protected static function _initAreaData()
    {
        if (empty(self::$areaData)) {

            $newRedis = new newCache(redisKeys::KEY_B_CACHE_CHINA_AREA);
            $data = $newRedis->getCache();
            if ($data) {
                self::$areaData = $data;
            } else {
                throw new \Exception('china_area数据未查询到');
            }
        }
    }


    public static function locationId2String($slocationId)
    {
        $locationIds = explode(",", $slocationId);
        $areas = array_map(function ($location) {
            return call_user_func_array([self::class, "locationId2Name"], [
                $location
            ]);
        }, $locationIds);

        return implode(' ', $areas);
    }

    public static function locationId2Array($slocationId)
    {
        $locationIds = explode(",", $slocationId);
        $areas = array_map(function ($location) {
            return call_user_func_array([self::class, "locationId2Name"], [
                $location
            ]);
        }, $locationIds);
        return $areas;
    }

    public static function locationId2Name($locationId)
    {
        self::_initAreaData();
        foreach (self::$areaData as $locationID => $areas) {
            foreach ($areas as $subLocationId => $subArea) {
                if ($subLocationId == $locationId) {
                    return $subArea;
                }
            }
        }
        return '';
    }

    public static function locationCodeOfProvince($code_id)
    {
        $data = [];
        self::_initAreaData();
        foreach (self::$areaData as $locationID => $subArea) {
            if ($locationID == $code_id) {
                $data = $subArea;
                break;
            }
        }
        return $data;
    }

    public static function getFormatLocation()
    {
        self::_initAreaData();
        $arr = [];
        foreach (self::$areaData as $locationID => $subArea) {
            if ($locationID == 86) {
                $data = $subArea;
                unset(self::$areaData['86']);
                break;
            }
        }
        foreach ($data as $code => $province) {
            foreach (self::$areaData[$code] as $area => $areas) {
                $arr1[$code][$area]['city_id'] = $area;
                $arr1[$code][$area]['city_name'] = $areas;

                if (isset(self::$areaData[$area])) {
                    foreach (self::$areaData[$area] as $key => $val) {
                        $arr2[$code][$area][] = [
                            'area_id' => $key,
                            'area_name' => $val
                        ];
                        unset(self::$areaData[$area]);
                    }
                    $arr1[$code][$area]['areas'] = array_values($arr2[$code][$area]);
                }
                $arr1[$code][$area]['areas'][] = ['area_id' => 999999, 'area_name' => '其他'];
                unset(self::$areaData[$code]);
            }
            $arr[] = [
                'province_id' => $code,
                'province_name' => $province,
                'citys' => array_values($arr1[$code])
            ];
            unset(self::$areaData[$code]);
        }
        return $arr;
    }

    /**
     * 获取省市区信息
     * @param $areaCode
     * @return array
     * @throws \Exception
     */
    public static function getInfoByCode($areaCode)
    {
        $result = [
            'province_id' => 0,
            'province_name' => '',
            'city_id' => 0,
            'city_name' => '',
            'area_id' => 0,
            'area_name' => ''
        ];
        $areaCodeArr = self::getAreaCodeArr($areaCode);
        if ($areaCodeArr) {
            $provinceCode = $areaCodeArr[0];
            $cityCode = $areaCodeArr[1];
            $countyCode = $areaCodeArr[2];
            self::_initAreaData();
            if (isset(self::$areaData['86'][$provinceCode]) && isset(self::$areaData[$provinceCode])) {
                $result['province_id'] = $provinceCode;
                $result['province_name'] = self::$areaData['86'][$provinceCode];
                if (isset(self::$areaData[$provinceCode][$cityCode])) {
                    $cityArr = array_keys(self::$areaData[$provinceCode]);
                    if (in_array($cityCode, $cityArr)) {
                        $result['city_id'] = $cityCode;
                        $result['city_name'] = self::$areaData[$provinceCode][$cityCode];
                        if (isset(self::$areaData[$cityCode][$countyCode])) {
                            $areaArr = array_keys(self::$areaData[$cityCode]);
                            if (in_array($countyCode, $areaArr)) {
                                $result['area_id'] = $countyCode;
                                $result['area_name'] = self::$areaData[$cityCode][$countyCode];
                            }
                        }
                    }
                } else if (isset(self::$areaData[$provinceCode][$countyCode])) {
                    $cityArr = array_keys(self::$areaData[$provinceCode]);
                    if (in_array($countyCode, $cityArr)) {
                        $result['city_id'] = $countyCode;
                        $result['city_name'] = self::$areaData[$provinceCode][$countyCode];
                    }
                }
            }
        }
        return $result;
    }

    /**
     * 返回省市区/县 code码
     * @param $areaCode code码
     * @return array
     */
    public static function getAreaCodeArr($areaCode)
    {
        if ($areaCode && strlen($areaCode) == 6) {
            $provinceCode = str_pad(substr($areaCode, 0, 2), 6, '0', STR_PAD_RIGHT);
            $cityCode = '';
            $countyCode = '';
            if (in_array($provinceCode, ['810000', '820000'])) {//香港澳门
                if ($areaCode != $provinceCode) {
                    $cityCode = (string)$areaCode;
                }
            } else {
                if ($areaCode != $provinceCode) {
                    $cityCode = str_pad(substr($areaCode, 0, 4), 6, '0', STR_PAD_RIGHT);
                }
                if ($areaCode != $provinceCode && $areaCode != $cityCode) {
                    $countyCode = (string)$areaCode;
                }
            }
            return [$provinceCode, $cityCode, $countyCode];
        } else {
            return [];
        }
    }


}